package algorithm.sort.base;

import java.security.SecureRandom;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class ArrayUtils {
    public static void swap(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }

    public static int[] newArrayWithInitialData(int length, int initial) {
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[initial] = initial;
        }
        return array;
    }

    public static int[] cloneArray(int[] data) {
        return newArrayByData(data);
    }

    public static int[] newArray(int length) {
        int[] array = new int[length];
        return array;
    }

    public static int[] newArrayByData(int... data) {
        int[] array = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            array[i] = data[i];
        }
        return array;
    }

    public static void shuffle(int[] data, int startInclude, int endExclude) {
        Random random = new SecureRandom();
        for (int i = startInclude; i < endExclude; i++) {
            swap(data, i, random.nextInt(startInclude, endExclude));
        }
    }

    public static String toString(int[] data) {
        StringBuffer   stringBuffer = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            stringBuffer.append(String.format("%3d", data[i]));
            if (i == data.length - 1) {
            } else {
                String.format(", ");
            }
        }
        return stringBuffer.toString();
    }
}
